﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Retete.Models;

namespace Retete.Controllers
{
    public class AutocompleteController : Controller
    {
        private ReteteEntities entities = new ReteteEntities();

        public ActionResult Retete()
        {
            string contains = Request["contains"];

            var result = from element in entities.Retete
                         where (element.Cod + " " + element.Nume).Contains(contains)
                         select new { Cod = element.Cod, Nume = element.Nume, ID = element.ID, Pret = element.Pret };

            return Json(result.Take(int.Parse(Request["maxRows"])).ToArray(), JsonRequestBehavior.AllowGet); 
        }
        public ActionResult Produse()
        {
            string contains = Request["contains"];
            return Json(entities.Produse.Where(element => (element.Cod + " " + element.Nume).Contains(contains)).Take(int.Parse(Request["maxRows"])).ToArray(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Tva()
        {
            var result = from element in entities.Receptii
                         select new { Valoare = element.Tva };

            return Json(result.Distinct(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Furnizori()
        {
            string contains = Request["contains"];

            var result = from element in entities.Receptii
                         where element.FurnizorNume.Contains(contains)
                         orderby element.FurnizorNume
                         select new { Nume = element.FurnizorNume, Adresa = element.FurnizorAdresa, Banca = element.FurnizorBanca, CodFiscal = element.FurnizorCodFiscal, CodRegistruComert = element.FurnizorCodRegistruComert, Cont = element.FurnizorCont, Judet = element.FurnizorJudet };

            return Json(result.Distinct().Take(int.Parse(Request["maxRows"])).ToArray(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult ElementeVanzare()
        {
            string contains = Request["contains"];

            var meniuri = from element in entities.Meniuri
                          where (element.Cod + " " + element.Denumire).Contains(contains) && (element.Cod + " " + element.Denumire).Contains("!)") == false
                          select new { Nume = element.Denumire + " (Meniu)", IDMeniu = element.ID, IDReteta = -1, IDProdus = -1, Pret = element.Pret, Cod = element.Cod };
            var retete = from element in entities.Retete
                         where (element.Cod + " " + element.Nume).Contains(contains)
                         select new { Nume = element.Nume + " (Reteta)", IDMeniu = -1, IDReteta = element.ID, IDProdus = -1, Pret = element.Pret, Cod = element.Cod };

            var result = meniuri.Union(retete).OrderBy(element => element.Nume);
            
            return Json(result.Take(int.Parse(Request["maxRows"])).ToArray(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Categorii()
        {
            string contains = Request["contains"];

            var categorii = from element in entities.Produse
                            where element.Categorie.Contains(contains)
                            select new { Categorie = element.Categorie };
            var result = categorii.Distinct();

            return Json(result.Take(int.Parse(Request["maxRows"])).ToArray(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Clienti()
        {
            string contains = Request["contains"];

            var clienti = from element in entities.Vanzari
                          where element.ClientNume.Contains(contains)
                          select new { ClientNume = element.ClientNume, ClientAdresa = element.ClientAdresa, ClientBanca = element.ClientBanca, ClientCodFiscal = element.ClientCodFiscal, ClientCodRegistruComert = element.ClientCodRegistruComert, ClientCont = element.ClientCont, ClientJudet = element.ClientJudet };
            var result = clienti.Distinct().OrderBy(element => element.ClientNume);

            return Json(result.Take(int.Parse(Request["maxRows"])).ToArray(), JsonRequestBehavior.AllowGet);
        }
    }
}